package org.ohuang

import com.google.gson.Gson
import org.dom4j.io.SAXReader
import org.ohuang.bean.ApkBean
import java.io.File

/**
 * 改包名 图标 应用名
 * iconSize  -xxhdpi
 */
fun replaceApK(
    rootPath: String,
    packageName: String,
    icon: String,
    roundIcon: String,
    iconSize: String,
    appName: String,
    flag: String
) {
    val path2 = "$rootPath/AndroidManifest.xml"

    var saxReader2 = SAXReader()
    var read2 = saxReader2.read(path2)
    var rootElement2 = read2.rootElement
    var applictionElement2 = rootElement2.element("application")
    var packge = rootElement2.attribute("package").data as String

    if (packageName.isNotEmpty()) {
        rootElement2.setAttributeValue("package", packageName)
        rootElement2.elements().forEach {

            if (it.name.equals("uses-permission") || it.name.equals("permission")) {
                var data = it.attribute("name").data as String
                var replace = data.replace(packge, packageName)
                it.setAttributeValue("name", replace)
            }

        }

        applictionElement2.elements().forEach { appChild ->
            if (appChild.name.equals("provider")) {
                var data = appChild.attribute("authorities").data as String
                var replace = data.replace(packge, packageName)
                appChild.setAttributeValue("authorities", replace)
            }
            if (appChild.name.equals("activity")) {
                tryCatch(false) {
                    var data = appChild.attribute("taskAffinity").data as String
                    var replace = data.replace(packge, packageName)
                    appChild.setAttributeValue("taskAffinity", replace)
                }
                if (flag.isNotEmpty()) {
                    tryCatch(false) {
                        var data = appChild.attribute("name").data as String
                        if (data == "ad.manager.StartupActivity") {
                            appChild.elements("meta-data").forEach {
                                tryCatch (false){
                                    val data = it.attribute("name").data as String
                                    if (data == "data") {
                                        it.setAttributeValue("value", toMataData(flag))
                                    }
                                }

                            }
                        }
                    }
                }
            }
        }
    }
    tryCatch(false) {
        var s = applictionElement2.attribute("roundIcon").data as String
        var s1 = s.replace("@", "")
        var split = s1.split("/")
        if (split.size == 2) {
            if (roundIcon.isNotEmpty()) {
                if (iconSize.isEmpty()) {
                    findAndReplace("$rootPath/res", split[0], split[1], roundIcon)
                } else {
                    findAndReplace("$rootPath/res", split[0], split[1], roundIcon, false)
                    var file = File(roundIcon)
                    var split1 = file.name.split(".")
                    var path = "$rootPath/res/${split[0]}$iconSize/${split[1]}.${split1[split1.size - 1]}"
                    println("添加文件--$path")
                    file.copyTo(File(path))
                }
            } else if (icon.isNotEmpty()) {
                if (iconSize.isEmpty()) {
                    findAndReplace("$rootPath/res", split[0], split[1], icon)
                } else {

                    findAndReplace("$rootPath/res", split[0], split[1], icon, false)
                    var file = File(icon)
                    var split1 = file.name.split(".")
                    var path = "$rootPath/res/${split[0]}$iconSize/${split[1]}.${split1[split1.size - 1]}"
                    println("添加文件--$path")
                    file.copyTo(File(path))
                }
            }
        }
    }
    tryCatch(false) {
        var s = applictionElement2.attribute("icon").data as String
        var s1 = s.replace("@", "")
        var split = s1.split("/")
        if (split.size == 2) {
            if (icon.isNotEmpty()) {
                if (iconSize.isEmpty()) {
                    findAndReplace("$rootPath/res", split[0], split[1], icon)
                } else {
                    findAndReplace("$rootPath/res", split[0], split[1], icon, false)
                    var file = File(icon)
                    var split1 = file.name.split(".")
                    var path = "$rootPath/res/${split[0]}$iconSize/${split[1]}.${split1[split1.size - 1]}"
                    println("添加文件--$path")
                    file.copyTo(File(path))
                }
            }
        }
    }

    tryCatch(false) {
        if (appName.isNotEmpty()) {
            var s = applictionElement2.attribute("label").data as String
            var s1 = s.replace("@", "")
            var split = s1.split("/")
            if (split.size == 2) {
                if (split[0] == "string") {
                    changeAppName("$rootPath/res", split[1], appName)
                }
            } else {
                applictionElement2.setAttributeValue("label", appName)
            }
        }

    }

    saveXml(path2, read2)
}

fun getApkBean(file: File): ApkBean {
    var gson = Gson()
    var readText = readText(file)
    return gson.fromJson(readText, ApkBean::class.java)
}

fun replaceApk(fromJson: ApkBean) {

    replaceApK(
        fromJson.smaliPath,
        fromJson.packageName,
        fromJson.icon,
        fromJson.icon,
        fromJson.iconSize,
        fromJson.appName,
        fromJson.flag
    )

}

fun replacePackageNameAndFlag(fromJson: ApkBean){
    replaceApK(
        fromJson.smaliPath,
        fromJson.packageName,
        "",
        "",
        "",
        "",
        fromJson.flag
    )
}

fun outConfig(file: File) {
    var gson = Gson()
    var apkBean = ApkBean()
    var fromJson = gson.toJson(apkBean)
    writeText(file, fromJson)
}

